package com.tdkj.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tdkj.model.base.BaseEntity;
import com.tdkj.model.common.BeanInformationSection;
import com.tdkj.model.shop.BeanCreditRating;
import com.tdkj.model.user.BeanAdmin;
import com.tdkj.model.user.BeanAdminLog;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.IBaseService;
import com.tdkj.service.IBaseService.TimeUnit;
import com.tdkj.service.IBaseService.VirtualCurrencyType;
import com.tdkj.service.shop.IBeanCreditRatingService;
import com.tdkj.util.Page;
import com.tdkj.util.PublicConfing;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class BaseAction extends ActionSupport {

	// 管理重新登录键
	public static final String ADMIN_RELOGIN = "login";

	private IBaseService baseService;

	private Page pager = new Page();
	private int maxPage = 10;
	private String pageUrl = "adminMainAction!findAll.action";

	// 查询信息
	private Map<String, String> queryInfo = new HashMap<String, String>();

	// 返回s信息
	private Map<String, Object> returnInfo = new HashMap<String, Object>();

	// 一些枚举
	private VirtualCurrencyType[] virtualCurrencyTypes = VirtualCurrencyType.values();

	private TimeUnit[] timeUnits = TimeUnit.values();

	public ActionContext getActionContext() {
		return ActionContext.getContext();
		// return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 管理员生成操作日志
	 * 
	 * @param info
	 */
	public void adminLogWrite(String info) {
		BeanAdminLog beanAdminLog = new BeanAdminLog();
		beanAdminLog.setAdmin(getCurrentAdmin());
		beanAdminLog.setIp(ServletActionContext.getRequest().getRemoteAddr());
		beanAdminLog.setCreatetime(new Timestamp(System.currentTimeMillis()));
		beanAdminLog.setAdminaction(info);
		this.baseService.save(beanAdminLog);
	}

	/**
	 * 
	 * @param criteria
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Criteria criteria, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(criteria, pager.getOffset() + 1, maxPage);

	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Query query, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(query, pager.getOffset() + 1, maxPage);
	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(String sql, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(sql, pager.getOffset() + 1, maxPage);
	}

	/**
	 * 得到当前管理员登录对象
	 * 
	 * @return
	 */
	public BeanAdmin getCurrentAdmin() {
		BeanAdmin beanAdmin = null;
		beanAdmin = (BeanAdmin) ActionContext.getContext().getSession().get(PublicConfing.ADMINISTRATOR_ADMIN_OBJ_STRING_CONFIG);
		return beanAdmin;
	}

	/**
	 * 得到当前登录用户
	 * 
	 * @return
	 */
	public BeanUser getCurrentUser() {
		BeanUser user = null;
		if (null != ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG)) {
			BeanUser temp = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
			user = this.getBaseService().findById(temp.getId(), BeanUser.class);
		}
		return user;
	}

	public Criteria createCriteria(Class<? extends BaseEntity> clazz) {
		return this.baseService.getBaseDao().getSession().createCriteria(clazz);
	}

	public Query createQuery(String hql) {
		return this.baseService.getBaseDao().getSession().createQuery(hql);
	}

	public IBaseService getBaseService() {
		return baseService;
	}

	@Resource
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public BeanUser getLoginUser() {
		BeanUser beanUser = null;
		// beanUser = (BeanUser)
		// ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		if (null != ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG)) {
			BeanUser temp = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
			beanUser = this.getBaseService().findById(temp.getId(), BeanUser.class);
		}
		return beanUser;
	}

	public Map<String, String> getQueryInfo() {
		return queryInfo;
	}

	public void setQueryInfo(Map<String, String> queryInfo) {
		this.queryInfo = queryInfo;
	}

	/**
	 * @return the virtualCurrencyTypes
	 */
	public VirtualCurrencyType[] getVirtualCurrencyTypes() {
		return virtualCurrencyTypes;
	}

	/**
	 * @param virtualCurrencyTypes
	 *            the virtualCurrencyTypes to set
	 */
	public void setVirtualCurrencyTypes(VirtualCurrencyType[] virtualCurrencyTypes) {
		this.virtualCurrencyTypes = virtualCurrencyTypes;
	}

	/**
	 * @return the timeUnits
	 */
	public TimeUnit[] getTimeUnits() {
		return timeUnits;
	}

	/**
	 * @param timeUnits
	 *            the timeUnits to set
	 */
	public void setTimeUnits(TimeUnit[] timeUnits) {
		this.timeUnits = timeUnits;
	}

	public Map<String, Object> getReturnInfo() {
		return returnInfo;
	}

	public void setReturnInfo(Map<String, Object> returnInfo) {
		this.returnInfo = returnInfo;
	}

	private List<BeanCreditRating> creditRatings;

	private IBeanCreditRatingService creditRatingService;

	@Resource
	public void setCreditRatingService(IBeanCreditRatingService creditRatingService) {
		this.creditRatingService = creditRatingService;
	}

	public List<BeanCreditRating> getCreditRatings() {
		if (creditRatings == null) {
			creditRatings = creditRatingService.findAll(BeanCreditRating.class);
		}
		return creditRatings;
	}

	public void setCreditRatings(List<BeanCreditRating> creditRatings) {
		this.creditRatings = creditRatings;
	}

	public List<BeanInformationSection> getInformationSections() {
		Criteria criteria = this.createCriteria(BeanInformationSection.class);
		criteria.addOrder(Order.asc("subscript"));
		return this.getBaseService().findBy(criteria, 1, 11).getList();
	}

}
